/**
 * Created by Administrator on 2018/1/25.
 */
let msg = require('../util/msg.js');
let userDao = require('../mysql/userDao.js');
let util = require('../util/util.js');
let rpc = require('../util/rpc.js');
let log = require('../util/log.js');


class ArenaA {
    static getInstance() {
        if (!ArenaA.instance) {
            ArenaA.instance = new ArenaA();
        }
        return ArenaA.instance;
    }

    constructor() {
        this._open = false;
        this._players = {};
        this._startTime = 0;
        this._endTime = 0;
    }

    start() {
        let now = (new Date()).toLocaleTimeString().replace(/^\D*/, '').split(':');
        let time = ((60 - 1 - parseInt(now[1]) ) * 3600) + parseInt(now[2]);
        this._open = true;
        this._startTime = util.timeStamp();
        this._endTime = this._startTime + time;
    }

    stop() {
        this._open = false;
    }

    inPlayer(id) {
        return !!this._players[id];
    }

    data() {
        let data = {};
        data.lostTime = this._endTime - util.timeStamp();
        data.players = [];
        for (let id in this._players) {
            let p = this._players[id];
            data.players.push({'id': p.id, 'name': p.name, 'avatar': p.avatar, 'point': p.point});
        }
        return data;
    }

    join(data) {
        this._players[data.id] = data;
        this._players[data.id].point = 1000;
        this._players[data.id].inArena = false;
        this._players[data.id].match = false;
        this._players[data.id].lock = false;
    }

    joinArena(id) {
        //this.broadcast('arenaJoin', {
        //    'id': this._players[id].id,
        //    'name': this._players[id].name,
        //    'avatar': this._players[id].avatar,
        //    'point': this._players[id].point
        //});
        this._players[id].inArena = true;
    }

    matchIn(id) {
        this._players[id].match = true;
    }

    matchOut(id) {
        this._players[id].match = false;
    }

    cron() {
        let _this = this;
        let matching = [];
        for (let i in this._players) {
            if (!this._players[i].inArena) {
                continue;
            }
            if (!this._players[i].match) {
                continue;
            }
            if (this._players[i].lock) {
                continue;
            }
            matching.push({'id': i, 'point': this._players[i].point});
        }
        if (matching.length < 3) {
            return false;
        }
        if (matching.length > 3) {
            matching = matching.sort(function (a, b) {
                return b.point - a.point;
            });
        }
        let data = {};
        data.user = [];
        data.mode = 1;
        data.lv = 7;
        for (let j in matching) {
            this._players[matching[j].id].lock = true;
            let userInfo = this._players[matching[j].id];
            data.user.push(userInfo);
        }
        rpc.game.send('arenaRoom', data, function (result) {
            _this.arenaRoomCB(result, matching)
        });
    }

    broadcast(key, obj) {
        for (let i in this._players) {
            if (!this._players[i].inArena) {
                continue;
            }
            msg.send(this._players[i].cid, this._players[i].uuid, key, obj);
        }
    }

    arenaRoomCB(result, userList) {
        if (result.code == 404) {
            for (let j in userList) {
                this._players[userList[j].id].lock = false;
            }
            log.d('房间服务器连接失败');
            return false;
        }
        if (result.code == 102) {
            let ids = [];
            for (let j in result.inGameId) {
                ids.push(j);
            }
            for (let j in userList) {
                if (ids.indexOf(userList[j].id) >= 0) {
                    msg.send(this._players[userList[j].id].cid, this._players[userList[j].id].uuid, 'matchInGame', {
                        'userId': userList[j].id,
                        'gameId': result.inGameId[userList[j]].gameId,
                        'local': result.inGameId[userList[j]].local,
                        'gameType': result.inGameId[userList[j]].gameType
                    });
                    //this._players[userList[j].id].inArena = true;
                    this._players[userList[j].id].match = false;
                } else {
                    this._players[userList[j].id].lock = false;
                }
            }
            return false;
        }
        if (result.code == 103) {
            let ids = [];
            for (let j in result.errId) {
                ids.push(j);
            }
            for (let j in userList) {
                if (ids.indexOf(userList[j]) >= 0) {
                    msg.send(this._players[userList[j].id].cid, this._players[userList[j].id].uuid, 'error', {'code': 10028});
                    //this._players[userList[j].id].inArena = true;
                    this._players[userList[j].id].match = false;
                } else {
                    this._players[userList[j].id].lock = false;
                }
            }
            return false;
        }
        if (result.code == 200) {
            let key = result.key;
            for (let j in userList) {
                let local = result.player[userList[j].id];
                msg.send(this._players[userList[j].id].cid, this._players[userList[j].id].uuid, 'matchGame', {
                    'userId': userList[j].id,
                    'gameId': key,
                    'local': local
                });
                //this._players[userList[j].id].inArena = true;
                this._players[userList[j].id].match = false;
            }
            log.p('room:' + key + ',data:' + JSON.stringify(result));
        }
    }

    gameOver(data) {
        for (let i in data.ids) {
            this._players[data.ids[i]].match = false;
            this._players[data.ids[i]].lock = false;
        }
        for (let i in data.point) {
            this._players[data.ids[i]].point = data.point[i];
        }
        console.log(this._players);
    }
}
ArenaA.instance = null;


module.exports = ArenaA;
